import api from '../api';
import { ErrorHandler } from './errorHandler';

/**
 * API工具类
 */
export class ApiHelper {
  /**
   * 安全的API调用
   * @param apiCall API调用函数
   * @param errorMessage 错误消息
   * @param showError 是否显示错误
   * @returns API响应或null
   */
  static async safeApiCall<T>(
    apiCall: () => Promise<T>,
    errorMessage: string = 'API调用失败',
    showError: boolean = true
  ): Promise<T | null> {
    return await ErrorHandler.safeExecute(apiCall, errorMessage, showError);
  }

  /**
   * 处理API响应
   * @param response API响应
   * @param successMessage 成功消息
   * @param errorMessage 错误消息
   * @returns 是否成功
   */
  static handleResponse(
    response: any,
    successMessage?: string,
    errorMessage: string = '操作失败'
  ): boolean {
    return ErrorHandler.handleApiResponse(response, successMessage, errorMessage);
  }

  /**
   * 通用的GET请求
   * @param url 请求URL
   * @param params 请求参数
   * @param errorMessage 错误消息
   * @returns 响应数据或null
   */
  static async get<T>(
    url: string,
    params?: any,
    errorMessage?: string
  ): Promise<T | null> {
    return await this.safeApiCall(
      () => api.get(url, { params }),
      errorMessage || `获取${url}数据失败`
    );
  }

  /**
   * 通用的POST请求
   * @param url 请求URL
   * @param data 请求数据
   * @param errorMessage 错误消息
   * @returns 响应数据或null
   */
  static async post<T>(
    url: string,
    data?: any,
    errorMessage?: string
  ): Promise<T | null> {
    return await this.safeApiCall(
      () => api.post(url, data),
      errorMessage || `提交${url}数据失败`
    );
  }

  /**
   * 通用的PUT请求
   * @param url 请求URL
   * @param data 请求数据
   * @param errorMessage 错误消息
   * @returns 响应数据或null
   */
  static async put<T>(
    url: string,
    data?: any,
    errorMessage?: string
  ): Promise<T | null> {
    return await this.safeApiCall(
      () => api.put(url, data),
      errorMessage || `更新${url}数据失败`
    );
  }

  /**
   * 通用的DELETE请求
   * @param url 请求URL
   * @param errorMessage 错误消息
   * @returns 响应数据或null
   */
  static async delete<T>(
    url: string,
    errorMessage?: string
  ): Promise<T | null> {
    return await this.safeApiCall(
      () => api.delete(url),
      errorMessage || `删除${url}数据失败`
    );
  }

  /**
   * 带响应处理的POST请求
   * @param url 请求URL
   * @param data 请求数据
   * @param successMessage 成功消息
   * @param errorMessage 错误消息
   * @returns 是否成功
   */
  static async postWithResponse(
    url: string,
    data?: any,
    successMessage?: string,
    errorMessage?: string
  ): Promise<boolean> {
    const response = await this.post(url, data, errorMessage);
    if (response) {
      return this.handleResponse(response, successMessage, errorMessage);
    }
    return false;
  }

  /**
   * 批量操作
   * @param operations 操作数组
   * @param successMessage 成功消息
   * @param errorMessage 错误消息
   * @returns 是否全部成功
   */
  static async batchOperations(
    operations: (() => Promise<any>)[],
    successMessage: string = '批量操作成功',
    errorMessage: string = '批量操作失败'
  ): Promise<boolean> {
    return await ErrorHandler.handleBatchOperations(operations, successMessage, errorMessage);
  }
}

/**
 * 简化的API调用函数
 */
export const safeApiCall = <T>(apiCall: () => Promise<T>, errorMessage?: string, showError?: boolean) =>
  ApiHelper.safeApiCall(apiCall, errorMessage, showError);

export const handleResponse = (response: any, successMessage?: string, errorMessage?: string) =>
  ApiHelper.handleResponse(response, successMessage, errorMessage);

export const apiGet = <T>(url: string, params?: any, errorMessage?: string) =>
  ApiHelper.get<T>(url, params, errorMessage);

export const apiPost = <T>(url: string, data?: any, errorMessage?: string) =>
  ApiHelper.post<T>(url, data, errorMessage);

export const apiPostWithResponse = (url: string, data?: any, successMessage?: string, errorMessage?: string) =>
  ApiHelper.postWithResponse(url, data, successMessage, errorMessage);

export const batchOperations = (operations: (() => Promise<any>)[], successMessage?: string, errorMessage?: string) =>
  ApiHelper.batchOperations(operations, successMessage, errorMessage);
